package com.wxmit.framework.data.permission.core.aspectj;



import com.wxmit.framework.data.permission.core.annotation.DataPermission;

import java.util.Objects;

/**
 * @Description DataPermissionContextHolder
 * @Author huangxiong
 * @Date 2022/7/15 16:44
 * @Version V1.0
 */
public class DataPermissionContextHolder {

    private static final ThreadLocal<DataPermission> DATA_PERMISSIONS =new ThreadLocal<>();

    /**
     * 获得当前的 DataPermission 注解
     *
     * @return DataPermission 注解
     */
    public static DataPermission get() {
        return DATA_PERMISSIONS.get();
    }

    /**
     * 入栈 DataPermission 注解
     *
     * @param dataPermission DataPermission 注解
     */
    public static void set(DataPermission dataPermission) {
        DATA_PERMISSIONS.set(dataPermission);
    }

    /**
     * 出栈 DataPermission 注解
     *
     * @return DataPermission 注解
     */
    public static DataPermission remove() {
        DataPermission dataPermission = DATA_PERMISSIONS.get();
        // 无元素时，清空 ThreadLocal
        if (Objects.nonNull(dataPermission)) {
            DATA_PERMISSIONS.remove();
        }
        return dataPermission;
    }

    /**
     * 清空上下文
     *
     * 目前仅仅用于单测
     */
    public static void clear() {
        DATA_PERMISSIONS.remove();
    }
}
